package com.GainLoop;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import org.alexd.jsonrpc.JSONRPCException;
import org.json.JSONException;

import com.GainLoop.Api.ApiResponseArray;
import com.GainLoop.Api.ApiResponseObject;
import com.GainLoop.Api.Categories;
import com.GainLoop.Api.Gains;
import com.GainLoop.Api.Tags;
import com.GainLoop.Api.Users;
import com.GainLoop.Api.Entity.Category;
import com.GainLoop.Api.Entity.Gain;
import com.GainLoop.Api.Entity.Tag;
import com.GainLoop.Api.Entity.Gain.STATUS;
import com.GainLoop.Api.Entity.User;
import com.GainLoop.Data.Registry;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class GainPage extends Activity{
	static final String GAIN_ID = "gain_id" ;
	
	@SuppressWarnings("unused")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gainpage);
		
		//Long gainId = savedInstanceState.getLong(GAIN_ID);
		Long gainId = getIntent().getExtras().getLong(GAIN_ID);
		
		createPage(gainId);
	}

	private void createPage(Long gainId) {
		TextView show_gain_desc = (TextView) findViewById(R.id.show_gain_desc);
		TextView show_gain_category = (TextView) findViewById(R.id.show_gain_category);
		TextView show_gain_tags = (TextView) findViewById(R.id.show_gain_tags);
		TextView show_gain_status = (TextView) findViewById(R.id.show_gain_status);
		
		final EditText add_gain_help_description = (EditText) findViewById(R.id.add_gain_help_description);
		Button gain_page_help_button = (Button) findViewById(R.id.gain_page_help_button);
		Gain gain = getGainById(gainId);
		
		
		Category category = getCategory(gain.getCategoryId());
		
		Tags tags = new Tags();
		ApiResponseArray<Tag> resp = null;
		try {
			resp = tags.getAllByGain(gain);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONRPCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ArrayList<Tag> tagsArray=resp.getData();
		
		StringBuffer allTagsStr = new StringBuffer();
		for(Tag curTag:tagsArray){
			allTagsStr.append(curTag.getName());
			allTagsStr.append(",");
		}
		show_gain_tags.append(" "+allTagsStr);

		String categoryName = null;
		if(category!=null){
			categoryName= category.getName();
		}
		else{
			categoryName = "";
		}
		final User gainUser = getGainUser(gain);
		
		
		show_gain_desc.setText(gainUser.getName() +" wish to "+ gain.getDescription());
		show_gain_category.append(" "+categoryName);
		
		gain_page_help_button.append(" "+gainUser.getName()+"!");
		gain_page_help_button.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		String helpDesc = add_gain_help_description.getText().toString();
        		Long toUserId = gainUser.getId();
        		Long curUserId = Registry.GetUser().getId();
        		//TODO : finish this...-Meir
            }
        });
		
		Object content = null;
	    try{
	      URL url = new URL("http://graph.facebook.com/"+gainUser.getFbId()+"/picture?type=square");
	      content = url.getContent();
	    }
	      catch(Exception ex)
	    {
	        ex.printStackTrace();
	    }
	    InputStream is = (InputStream)content;
	    Drawable image = Drawable.createFromStream(is, "src");
	    //img.setImageDrawable(image);
	    show_gain_desc.setCompoundDrawablesWithIntrinsicBounds(null, null, image, null);
	    show_gain_desc.setCompoundDrawablePadding(100);
	    
	    show_gain_status.append(Gain.statusToString(gain.getStatus()));
	    
	}

	private Category getCategory(long categoryId) {
		Categories categories = new Categories();
		ApiResponseObject<Category> resp = null;
		try {
			resp = categories.getById(categoryId);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONRPCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Category category = null;
		category=resp.getData();
		return category;
	}

	private Gain getGainById(Long gainId) {
		Gains gains = new Gains();
		ApiResponseObject<Gain> resp = null;
		try {
			resp = gains.getById(gainId);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONRPCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Gain gain = null;
		gain=resp.getData();
		return gain;
	}

	private User getGainUser(Gain gain) {
		Users users = new Users();
		ApiResponseObject<User> resp = null;
		try {
			resp = users.getById(gain.getToUserId());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONRPCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		User user = null;
		user=resp.getData();
		return user;
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
    	Log.v("menu", "inflating menu..");
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        
        menu.setGroupVisible(R.id.group1, false);
        menu.setGroupVisible(R.id.group2, false);
        menu.setGroupVisible(R.id.group3, false);
      
        
        return true;
    }
	
	public boolean onOptionsItemSelected (MenuItem item){
    	Toast toast;
    	Intent intent;
		 switch (item.getItemId()) {
		    case R.id.menu_profile_button:
		    	intent=new Intent(GainPage.this,UserPage.class);
		    	GainPage.this.startActivity(intent);
		        return true;
		    case R.id.help_button:
		    	toast = Toast.makeText(GainPage.this,"help", Toast.LENGTH_LONG);
				toast.show();
		        return true; 
		    case R.id.menu_search_button:
		    	intent=new Intent(GainPage.this,GainSearch.class);
		    	GainPage.this.startActivity(intent);
		        return true;
		    case R.id.menu_home_button:
		    	intent=new Intent(GainPage.this,Home.class);
		    	GainPage.this.startActivity(intent);
		        return true;   
		    case R.id.menu_new_gain_button:
		    	intent=new Intent(GainPage.this,AddRequest.class);
		    	GainPage.this.startActivity(intent);
		        return true;      
		        
		    default:
		        return super.onOptionsItemSelected(item);
		    }
		
    }
	
}
